import {Component, Input, OnInit} from "@angular/core";
import {LocalDataSource} from "ng2-smart-table";
import {BaseRequest} from "../../utils/BaseRequest";
import {map} from "rxjs/operators";
import {DevExtraService} from "../../energy/api/api/devExtra.service";
import {NbDateService} from "@nebular/theme";
import {ResponseDevActionLog} from "../../energy/api/model/responseDevActionLog";
import {CustomBooleanRender} from "../../utils/CustomBooleanRender";

@Component({
  selector:'device-log-component',
  templateUrl:'./device-log.component.html',
  styles:[`
      nb-card {
          transform: translate3d(0, 0, 0);
      }

      button {
          margin-top: 5px;
          margin-bottom: 5px;
      }
      .margin {
          margin: 10px;
      }
  `]
})
export class DeviceLogComponent implements OnInit {
  @Input("showFilter") showFilter?:boolean;
  @Input("devNo") devNo;
  action:number;
  startTime:Date;
  endTime:Date;
  operator:string;
  source: DataDataSource;
  settings = {
    pager:{
      display:true,
      perPage:20
    },
    hideSubHeader: true,
    actions: {
      add: false,
      edit: false,
      delete: false,
      position: 'right',
    },
    columns: {
      devNo: {
        title: '编号',
        type: 'string',
      },
      action: {
        title: '操作',
        type: 'string',
      },
      isAuto:{
        title: '自动操作',
        type: 'custom',
        renderComponent: CustomBooleanRender
      },
      createTime:{
        title: '时间',
        type: 'string'
      },
      operator:{
        title: '操作人',
        type: 'string'
      },
      extInfo: {
        title: '额外信息',
        type: 'string',
      },
    },
  };
  constructor(private api:DevExtraService,private dateService: NbDateService<Date>){
    this.startTime = this.dateService.addDay(this.dateService.today(), -30);
    this.endTime = this.dateService.today()
  }
  ngOnInit(): void {
    this.source = new DataDataSource(this.api, this.dateService,this.devNo,this.action,this.operator,this.startTime,this.endTime);
  }

  filter(){
    this.source.devNo = this.devNo;
    this.source.action = this.action;
    this.source.operator = this.operator;
    this.source.startTime = this.startTime;
    this.source.endTime = this.endTime;
    this.source.setPage(1);
  }
}


export class DataDataSource extends LocalDataSource {
  all: number = -1;
  constructor(private api: DevExtraService,private dateService: NbDateService<Date>,public devNo?:string,public action?:number,public operator?:string,public startTime?:Date,public endTime?:Date) {
    super()
  }

  count(): number {
    return this.all;
  }

  getElements(): Promise<any> {
    const query = {
      pageIndex: (this.pagingConf['page'] - 1) * this.pagingConf['perPage'],
      pageSize: this.pagingConf['perPage'],
      devNo:this.devNo,
      action:this.action,
      operator:this.operator
    };
    if(this.startTime!=null)
        query['startTime'] = this.dateService.format(this.startTime,"yyyyMMdd");
    if(this.endTime!=null)
        query['endTIme'] = this.dateService.format(this.endTime,"yyyyMMdd");
    return this.api.requestDevActionLog(new BaseRequest(query)).pipe(map((res:ResponseDevActionLog)=>{
      this.all = res.dataCount;
      return res.data.map(value=>{
        if(value.action==0 || value.action==1){
          value.isAuto = true;
        }else{
          value.isAuto = false;
        }
        return value;
      });
    })).toPromise();
  }
}

